// pages/userAccount/depositRecorder/depositRecorder.js
const accountService = require('../../../service/accountService.js');
const utils = require('../../../utils/util.js');
// load promise 
require('../../../utils/wx-pro.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    historyList: [],
    pageParam: {
      pageNo: 1,
      pageSize: 20
    },
    totalPage: '',
    isEmpty: true,
    reqDelay: false,
    showToast: false,
    toastMsg: ''
  },
  
  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getListDepositRecorder()
  },
  replaceChange(str) {
    //log(str)
    var s = str.replace(/\//g, ".")
    s = s.substring(0, s.length - 3)
    return s
  },
  getListDepositRecorder() {
    this.setData({
      reqDelay: true
    });
    var params = this.data.pageParam
    accountService.listDepositRecorder(params).then(data => {
      if (data.code == 200) {
        this.setData({
          totalPage: data.response.page.totalPage
        })

        var array = data.response.depositRecorderList
        var historyList = [];
        for (var idx in array) {
          var subject = array[idx];
          var temp = {
            createTime: this.replaceChange(new Date(subject.createTime).toLocaleString('chinese', { hour12: false })),
            remark: subject.remark,
            recorderMonney: subject.recorderMonney
          }
          historyList.push(temp)
        }
        var totalHistoryList = {}

        //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
        if (!this.data.isEmpty) {
          totalHistoryList = this.data.historyList.concat(historyList);
        }
        else {
          totalHistoryList = historyList;
          this.data.isEmpty = false;
        }
        this.setData({
          historyList: totalHistoryList,
          reqDelay: false
        });
        wx.hideNavigationBarLoading();
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch(() => {
        console.log('error')
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  getMoreDate: function () {
    if (!this.data.reqDelay){
      if (this.data.pageParam.pageNo >= this.data.totalPage) {
        console.log("没有了")
      } else {
        console.log(2222222222)
        wx.showNavigationBarLoading()
        this.setData({
          pageParam: {
            pageNo: this.data.pageParam.pageNo + 1,
            pageSize: 20
          },
        })
        this.getListDepositRecorder()
      }
    }
  }
})